﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using vJine.Core;
using vJine.Lua.GUI.BLL;

namespace vJine.Lua.GUI {
    /// <summary>
    /// WinFlowChart.xaml 的交互逻辑
    /// </summary>
    public partial class WinFlow : Window {

        public WinFlow() {
            InitializeComponent();

            this.btnCmdA_TOP.Command = this.drc;
            this.btnCmdA_TOP.CommandParameter = "CMD_ALIGN_TOP";
            this.btnCmdA_BOTTOM.Command = this.drc;
            this.btnCmdA_BOTTOM.CommandParameter = "CMD_ALIGN_BOTTOM";
            this.btnCmdA_VCENTER.Command = this.drc;
            this.btnCmdA_VCENTER.CommandParameter = "CMD_ALIGN_VCENTER";
            this.btnCmdA_LEFT.Command = this.drc;
            this.btnCmdA_LEFT.CommandParameter = "CMD_ALIGN_LEFT";
            this.btnCmdA_RIGHT.Command = this.drc;
            this.btnCmdA_RIGHT.CommandParameter = "CMD_ALIGN_RIGHT";
            this.btnCmdA_HCENTER.Command = this.drc;
            this.btnCmdA_HCENTER.CommandParameter = "CMD_ALIGN_HCENTER";

            this.btnCmd_Save.Command = this.drc;
            this.btnCmd_Save.CommandParameter = "CMD_SAVE";
            this.btnCmd_Load.Command = this.drc;
            this.btnCmd_Load.CommandParameter = "CMD_LOAD";

            this.btnCmd_Run.Command = this.drc;
            this.btnCmd_Run.CommandParameter = "CMD_RUN";

            this.show_message = (string msg, string title, MessageBoxButton button, MessageBoxImage icon) => {
                MessageBox.Show(this, msg, title, button, icon);
            };
        }

        AppGUI appGUI = null;

        Action<string, string, MessageBoxButton, MessageBoxImage> show_message = null;
        private void Label_MouseDown(object sender, MouseButtonEventArgs e) {
            NodeConfig nConfig = this.trvConfigs.SelectedItem as NodeConfig;
            if (nConfig == null || nConfig.ui_ignore) {
                return;
            }

            if (Keyboard.IsKeyDown(Key.LeftCtrl)) {
                this.drc.doDrag(nConfig); e.Handled = true;
            }
        }

        private void Label_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
            NodeConfig nConfig = this.trvConfigs.SelectedItem as NodeConfig;
            if (nConfig == null || nConfig.ui_ignore || string.IsNullOrEmpty(nConfig.gui)) {
                return;
            }
            WinGUI.load(this.appGUI, nConfig.gui, this, this.luaContext, nConfig);
            this.set_bound();
        }

        LuaContext luaContext;
        NodeConfig luaConfig;
        ConfigManager configManager;
        public WinFlow set_bound(LuaContext luaContext, NodeConfig luaConfig, ConfigManager configManager) {
            this.appGUI = new AppGUI(luaContext);
            this.drc.winMain = this;
            this.drc.appGUI = this.appGUI;

            this.nodeView.on_running += (bool is_running) => {
                this.toolPannel.IsEnabled = !is_running;
            };

            this.DataContext = luaConfig;

            this.luaContext = luaContext;
            this.luaConfig = luaConfig;
            this.configManager = configManager;

            this.drc.luaContext = luaContext;
            this.drc.nodeConfigs = luaConfig;
            this.drc.configManager = new ConfigManager();
            this.drc.set_context();

            this.trvConfigs.Items.Clear();
            this.trvConfigs.ItemsSource = this.luaConfig;

            return this;
        }

        internal void set_bound() {
            NodeConfig luaConfig = this.trvConfigs.SelectedItem as NodeConfig;
            if (luaConfig == null) {
                return;
            }
            this.nodeView.set_bound(this.appGUI, this.luaContext, luaConfig);
        }

        private void trvConfigs_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) {
            NodeConfig luaConfig = e.NewValue as NodeConfig;
            this.nodeView.set_bound(this.appGUI, this.luaContext, luaConfig);
        }

        private void trvConfigs_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
            this.tbiNodeView.IsSelected = true; //切换到程序视图
        }

        private void cm_Click(object sender, RoutedEventArgs e) {
            Control uc = sender as Control;
            string txtURI = uc.Tag as string;
            Clipboard.SetText(txtURI);
            MessageBox.Show(this, txtURI);
        }
    }
}
